const userService = require('../services/userService')

// 获取所有用户
const getAllUsers = async (req, res) => {
  try {
    const users = await userService.getAllUsers()
    res.json({
      status: 0,
      data: users
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

// 创建新用户
const createUser = async (req, res) => {
  try {
    const newUser = await userService.createUser(req.body)
    res.json({
      status: 0,
      data: newUser
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const getUserById = async (req, res) => {
  try {
    const user = await userService.getUserById(req.query.id)
    res.json({
      status: 0,
      data: user
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const getUserByName = async (req, res) => {
  try {
    const user = await userService.getUserByName(req.query.name)
    res.json({
      status: 0,
      data: user
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

module.exports = {
  getAllUsers,
  createUser,
  getUserById,
  getUserByName
}
